package com.nanohadoop.mapreduce.core;

import java.io.IOException;
import java.util.Iterator;

/**
 * Reducer 上下文,用于写出结果
 */
public class ReducerContext<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
    private KEYIN currentKey;
    private Iterator<VALUEIN> currentValues;
    private ReducerRecordWriter<KEYOUT, VALUEOUT> writer;
    private float reduceProgress = 0.0f;

    public ReducerContext(ReducerRecordWriter<KEYOUT, VALUEOUT> writer) {
        this.writer = writer;
    }

    public ReducerContext() {

    }

    public void write(KEYOUT key, VALUEOUT value) throws IOException {
        writer.write(key, value);
    }

    public boolean nextKey() throws IOException, InterruptedException {
        if (hasNextKey()) {
            currentKey = getNextKey();
            currentValues = getNextValues();
            updateProgress();
            return true;
        }
        return false;
    }

    public KEYIN getCurrentKey() throws IOException, InterruptedException {
        return currentKey;
    }

    public Iterator<VALUEIN> getValues() throws IOException, InterruptedException {
        return currentValues;
    }

    public float getProgress() {
        return reduceProgress;
    }

    private boolean hasNextKey() {
        // 由具体实现类提供
        return false;
    }

    private KEYIN getNextKey() {
        // 由具体实现类提供
        return null;
    }

    private Iterator<VALUEIN> getNextValues() {
        // 由具体实现类提供
        return null;
    }

    private void updateProgress() {
        // 更新处理进度
    }
}